import util from '@yunke/core/util';
import CONFIG from './config';

const angular = require('angular');
const ui = require('@yunke/core/ui');

const app = angular.module('myBackendApp');

app.register.controller('revenueController', ($scope, $http, $routeParams) => {
    $scope.revenueType = $routeParams.type;

    const config = CONFIG[$scope.revenueType];
    $scope.title = config.title;
    $scope.cols = config.cols;

    $scope.dialogTemplate = require('./dialog/revenueRecognized.html');
    $scope.pageOptions = {
        pageSize: 10,
        pageIndex: 1,
    };

    const getRevenueListByPage = function (params = {}) {
        const {
            revenueType
        } = $scope;
        return $http({
            method: 'GET',
            url: '/api/index.php?r=revenue/get-revenue-list-by-page',
            params: {
                ...$scope.pageOptions,
                ...params,
                contractStatus: 2,
                isCancelled: 0,
                type: revenueType === 'closed' ? '' : revenueType,
                closeStatus: revenueType === 'closed' ? 'closed' : (revenueType === 'unrecognized' ? 'unclosed' : ''),
            }
        })
            .then((res) => {
                const {
                    items,
                    total
                } = res.data.data;
                $scope.list = items;
                $scope.total = total;
            })
    };

    getRevenueListByPage();

    $scope.changePage = () => {
        getRevenueListByPage();
    };

    $scope.editRecognitionDate = false;

    if ($scope.revenueType === 'unrecognized' || $scope.revenueType === 'recognized') {
        $http.get('/api/index.php?r=revenue/get-recognition-date')
            .then((res) => {
                const {
                    recognition_begin: recognitionBegin,
                    recognition_end: recognitionEnd,
                } = res.data.data || {};
                $scope.recognitionBegin = recognitionBegin;
                $scope.recognitionBeginUI = recognitionBegin;
                $scope.recognitionEnd = recognitionEnd;
                $scope.recognitionEndUI = recognitionEnd;
            });

        $scope.setRecognitionDate = () => {
            ui.loading();
            return $http.post('/api/index.php?r=revenue/set-recognition-date', {
                recognition_begin: $scope.recognitionBeginUI,
                recognition_end: $scope.recognitionEndUI,
            })
                .then((res) => {
                    $scope.pageOptions.pageIndex = 1;
                    getRevenueListByPage()
                        .then(() => {
                            const {
                                data
                            } = res;
                            if (data.data) {
                                util.notify('修改成功');
                                $scope.editRecognitionDate = false;
                                $scope.recognitionBegin = $scope.recognitionBeginUI;
                                $scope.recognitionEnd = $scope.recognitionEndUI;
                            } else {
                                util.alert(data.errMsg);
                            }
                        })
                })
                .catch(() => {
                    ui.removeLoading();
                })
        };

        $http.get('/api/index.php?r=/revenue/get-recent-recognition-user')
            .then((res) => {
                const {
                    recognition_date: recognitionDate,
                    recognition_by: recognitionSetBy
                } = res.data.data;
                $scope.recognitionDate = recognitionDate;
                $scope.recognitionSetBy = recognitionSetBy;
            })
    }

    $scope.onInitDialog = ($dialog, $dialogScope) => {
        $dialogScope.revenueRecognition = (recognitionType) => {
            ui.loading();
            $http.post('/api/index.php?r=revenue/revenue-recognition', {
                recognition_type: recognitionType,
            })
                .then((res) => {
                    const data = res.data.data || {};

                    const {
                        date: {
                            success: dateSuccess = 0,
                            fail: dateFail = 0
                        } = {},
                        disposable: {
                            success: disposableSuccess = 0,
                            fail: disposableFail = 0
                        } = {},
                        ad: {
                            success: adSuccess = 0,
                            fail: adFail = 0
                        } = {},
                        time,
                        user
                    } = data;

                    const tips = ['<div>结果</div>'];

                    if (dateSuccess + dateFail > 0) {
                        tips.push(`<div>按租期确认结转：成功&nbsp;<span>${dateSuccess}</span>，失败&nbsp;<span>${dateFail}</span></div>`);
                    }

                    if (disposableSuccess + disposableFail > 0) {
                        tips.push(`<div>按租期确认结转：成功&nbsp;<span>${disposableSuccess}</span>，失败&nbsp;<span>${disposableFail}</span></div>`);
                    }

                    if (adSuccess + adFail > 0) {
                        tips.push(`<div>广告：成功&nbsp;<span>${adSuccess}</span>，失败&nbsp;<span>${adFail}</span></div>`);
                    }

                    if (tips.length === 1) {
                        tips.push('<div>暂无结转记录</div>');
                    }

                    $dialogScope.data.recognitionDate = time;
                    $scope.recognitionDate = time;
                    $dialogScope.data.recognitionSetBy = user;
                    $scope.recognitionSetBy = user;

                    $scope.pageOptions.pageIndex = 1;
                    getRevenueListByPage()
                        .then(() => {
                            $dialog.remove();
                            util.notify(`<div>结转成功：${res.data.data}</div>`);
                        })
                })
                .catch(() => {
                    ui.removeLoading();
                })
        }
    };

    $scope.downloadExcel = () => {
        ui.loading();

        const {
            revenueType
        } = $scope;
        return $http.get('/api/index.php?r=/revenue/export-revenue', {
            params: {
                ...$scope.pageOptions,
                contractStatus: 2,
                isCancelled: 0,
                type: revenueType,
                closeStatus: revenueType === 'closed' ? 'closed' : (revenueType === 'unrecognized' ? 'unclosed' : ''),
            }
        })
            .then((res) => {
                ui.removeLoading();
                util.alert(`<div>导出成功：${res.data.data}</div>`)
            })
            .catch(() => {
                ui.removeLoading();
            })
    };
});
